package com.hefei.hight.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author caizhengjun
 * @date 2020/9/5 2:58 下午
 */
public class ProxyStudent implements MethodInterceptor {

    private Object target;

    public ProxyStudent(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(target.getClass());

        enhancer.setCallback(this);

        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        System.out.println("开始事务");

        method.invoke(target, args);

        System.out.println("执行事务");

        return null;
    }
}
